﻿App.Stat = function() {

    return {

        getStore: function() {
            var store = new Ext.data.DirectStore({
                directFn: ProductAction.stat,
                paramOrder: "start,limit,sort,dir,query,endDate",
                paramNames: {
                    dir: "dir",
                    sort: "sort",
                    start: "start",
                    limit: "limit",
                    query: "query",
                    endDate: "endDate"
                },
                baseParams: {
                    dir: "",
                    sort: "",
                    start: 0,
                    limit: 50,
                    query: "",
                    endDate: Ext.util.Format.date(new Date(), "Y-m-d")
                },
                paramsAsHash: true,
                totalProperty: 'totalCount',
                idProperty: 'id',
                root: 'result',
                autoLoad: true,
                remoteSort: true,
                fields: [{
                    name: 'id',
                    type: 'int'
                },
                {
                    name: 'name'
                },
                {
                    name: 'spec'
                },
                {
                    name: 'price',
                    type: 'float'
                },
                {
                    name: 'in',
                    type: 'int'
                },
                {
                    name: 'out',
                    type: 'int'
                },
                {
                    name: 'stock',
                    type: 'int'
                }]
            });
            return store;
        },

        highLight: function(value) {
            var key = App.Stat.grid.store.baseParams.query;
            var v = value.toString();
            if (Ext.isString(v) & Ext.isString(key)) {
                if (key.length > 0) {
                    var re = new RegExp(key, "g");
                    return v.replace(re, "<span style='color:red;background-color:yellow;'>" + key + "</span>");
                } else {
                    return v
                }
            } else {
                return v;
            }
        },

        createGrid: function(id) {

            var panel = Ext.getCmp(id);

            panel.body.dom.innerHTML = "";

            this.dateField = new Ext.form.DateField({
                name: 'endDate',
                maxlength: 20,
                value: Ext.util.Format.date(new Date(), "Y-m-d"),
                allowBlank: false,
                anchor: '90%',
                enableKeyEvents: true,
                format: 'Y-m-d',
            });

            this.grid = new Ext.grid.GridPanel({
                tbar: new Ext.PagingToolbar({
                    pageSize: 50,
                    displayInfo: true,
                    store: this.store,
                    items: ['-', new Ext.ux.form.SearchField({
                        store: this.store,
                        width: 110
                    }), '-', '截止日期：', this.dateField, {
                        text: '统计',
                        scope: this,
                        handler: function() {
                            this.store.baseParams.endDate = this.dateField.getValue();
                            this.store.reload();
                        }
                    }]
                }),
                store: this.store,
                columns: [{
                    header: "编号",
                    width: 100,
                    sortable: true,
                    dataIndex: 'id',
                    renderer: this.highLight
                },
                {
                    header: "产品名称",
                    width: 100,
                    sortable: true,
                    dataIndex: 'name',
                    renderer: this.highLight
                },
                {
                    header: "产品规格",
                    width: 100,
                    sortable: true,
                    dataIndex: 'spec'
                },
                {
                    header: "单价",
                    width: 100,
                    sortable: true,
                    dataIndex: 'price',
                    align: 'right',
                    renderer: function(v) {
                        return Ext.util.Format.number(v, "0.00");
                    }
                },
                {
                    header: "进仓量",
                    width: 100,
                    sortable: true,
                    dataIndex: 'in',
                    align: 'right',
                    renderer: function(v) {
                        return Ext.util.Format.number(v, "0");
                    }
                },
                {
                    header: "出仓量",
                    width: 100,
                    sortable: true,
                    dataIndex: 'out',
                    align: 'right',
                    renderer: function(v) {
                        return Ext.util.Format.number(v, "0");
                    }
                },
                {
                    header: "库存",
                    width: 100,
                    sortable: true,
                    dataIndex: 'stock',
                    align: 'right',
                    renderer: function(v) {
                        return Ext.util.Format.number(v, "0");
                    }
                }]
            });

            panel.add(this.grid);

        },

        render: function(id) {
            if (!this.store) {
                this.store = this.getStore();
            }; //if(!this.store)

            this.createGrid(id);

        }

    }; //return
} (); //function
